package mainFragment.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import java.util.ArrayList;

import mainFragment.bean.CollegeVolunteerBean;
import view.MyImageLoader;
import volunteer.activity.Volunteer;

/**
 * Created by Administrator on 2017/3/3.
 */

public class CollegeIndexOAAdapter extends BaseAdapter {
    //TODO
    //v1.3.0再删掉这里的代码
    private Context context;
    private ArrayList<ArrayList<CollegeVolunteerBean>> arrayList;

    private static final String TAG = "CollegeIndexOAAdapter";

    public CollegeIndexOAAdapter(Context context, ArrayList<ArrayList<CollegeVolunteerBean>> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyHolder myHolder;

        myHolder = new MyHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.college_index_oa_item, null);
        myHolder.college_index_oa_item_text = (TextView) convertView.findViewById(R.id.college_index_oa_item_text);
        myHolder.college_index_oa_item_grid = (GridView) convertView.findViewById(R.id.college_index_oa_item_grid);
        if (position == 0) {
            myHolder.college_index_oa_item_text.setText(context.getResources().getString(R.string.main_page_item5));
        } else if (position == 1) {
            myHolder.college_index_oa_item_text.setText("学长学姐");
        }
        convertView.setTag(convertView);

        if (arrayList.size() > 0) {
            MyAdapter myAdapter = new MyAdapter(arrayList.get(position), position);
            myHolder.college_index_oa_item_grid.setAdapter(myAdapter);
            setPullLvHeight(myHolder.college_index_oa_item_grid, myAdapter);
            myHolder.college_index_oa_item_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position1, long id) {
                    Intent i = new Intent();
                    i.setClass(context, Volunteer.class);
                    i.putExtra("id", arrayList.get(position).get(position1).getId());
                    if (position == 0) {
                        //vip
                        i.putExtra("type", "vip");
                    } else {
                        //非vip
                        i.putExtra("type", "normal");
                    }
                    context.startActivity(i);
                }
            });
        }
        return convertView;
    }

    class MyHolder {
        TextView college_index_oa_item_text;
        GridView college_index_oa_item_grid;
    }

    class MyAdapter extends BaseAdapter {
        private ArrayList<CollegeVolunteerBean> collegeVolunteerBeen;
        private int vipCount = 0;
        private int normalCount = 0;
        private int position;

        public MyAdapter(ArrayList<CollegeVolunteerBean> collegeVolunteerBeen, int position) {
            this.collegeVolunteerBeen = collegeVolunteerBeen;
            this.position = position;
            for (int i = 0; i < collegeVolunteerBeen.size(); i++) {
                if (collegeVolunteerBeen.get(i).isVip()) {
                    vipCount++;
                } else {
                    normalCount++;
                }
            }
        }

        @Override
        public int getCount() {
            if (position == 0) {
                return vipCount;
            } else {
                return normalCount;
            }
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHolder1 myholder;
            if (convertView == null) {
                myholder = new MyHolder1();
                convertView = LayoutInflater.from(context).inflate(R.layout.college_index_oa_item_grid, null);
                myholder.name = (TextView) convertView.findViewById(R.id.college_index_oa_item_grid_name);
                myholder.brief = (TextView) convertView.findViewById(R.id.college_index_oa_item_grid_brief);
                myholder.college_index_oa_item_grid_ima = (ImageView) convertView.findViewById(R.id.college_index_oa_item_grid_ima);
                convertView.setTag(myholder);
            } else {
                myholder = (MyHolder1) convertView.getTag();
            }

            myholder.name.setText(collegeVolunteerBeen.get(position).getName());
            myholder.brief.setText(collegeVolunteerBeen.get(position).getBrief());
            MyImageLoader.showImage(collegeVolunteerBeen.get(position).getHeadimg_url(), myholder.college_index_oa_item_grid_ima);
            return convertView;
        }
    }

    private class MyHolder1 {
        TextView name;
        ImageView college_index_oa_item_grid_ima;
        TextView brief;
    }

    private void setPullLvHeight(GridView pull, Adapter adapter) {
        int totalHeight = 0;
        float row1 = (float) (adapter.getCount() / 4.0);
        int row = (int) Math.ceil(row1);
        if (row > 0) {
            View listItem = adapter.getView(0, null, pull);//获取其中的view
            listItem.measure(0, 0); //计算子项View 的宽高
            totalHeight += (pull.getVerticalSpacing() + listItem.getMeasuredHeight()) * row;//统计所有子项的总高度
        }
        ViewGroup.LayoutParams params = pull.getLayoutParams();
        params.height = totalHeight;
        pull.setLayoutParams(params);
    }

}
